package com.sds.listener;

import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.servlet.GuiceServletContextListener;
import com.sds.ai.impl.provider.AIMLResponseProvider;
import com.sds.module.KimModule;
import org.aitools.programd.Core;
import org.aitools.programd.util.URLTools;
import org.apache.log4j.Logger;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import java.net.MalformedURLException;
import java.net.URL;


public class KimGuiceListener extends GuiceServletContextListener {


    private static Injector INJECTOR = Guice.createInjector(new KimModule());
    private final Logger LOGGER = Logger.getLogger(getClass().getName());

    public KimGuiceListener() {
        super();
    }

    @Override
    protected Injector getInjector() {
        return INJECTOR;
    }

    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        super.contextInitialized(servletContextEvent);
        try {
            ServletContext context = servletContextEvent.getServletContext();
            URL base = context.getResource("/programd/");
            INJECTOR.getInstance(AIMLResponseProvider.class).setChatCore(new Core(base, URLTools.contextualize(base, "conf/core.xml")));
        } catch (MalformedURLException e) {
            LOGGER.error(e.getMessage(), e);
        }

    }

}
